package cn.com.gaobb.service.impl;

import cn.com.gaobb.dto.ResultDTO;
import cn.com.gaobb.entity.TClient;
import cn.com.gaobb.mapper.TClientMapper;
import cn.com.gaobb.service.ClientService;
import cn.com.gaobb.utils.BeanHelper;
import cn.com.gaobb.utils.IdBuilderUtil;
import cn.com.gaobb.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private TClientMapper clientMapper;
    @Override
    public ResultDTO<ResIdVO> add(ReqClientAddVO reqVO) {
        TClient tClient = BeanHelper.copyProperties(reqVO, TClient.class);
        tClient.setId(IdBuilderUtil.getId());
        tClient.setCreateTime(new Date());
        tClient.setModifyTime(new Date());
        clientMapper.insertSelective(tClient);
        return ResultDTO.okOf(new ResIdVO(tClient.getId()));
    }

    @Override
    public ResultDTO update(ReqClientUpdateVO vo) {
        TClient tClient = BeanHelper.copyProperties(vo, TClient.class);
        tClient.setModifyTime(new Date());
        clientMapper.updateByPrimaryKeySelective(tClient);
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO deleteList(ReqClientDeleteListVO vo) {
        if(CollectionUtils.isEmpty(vo.getIdList())){
            return ResultDTO.errorOf(1001, "请选择要删除的客户");
        }
        vo.getIdList().forEach(id->{
            clientMapper.deleteByPrimaryKey(id);
        });
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResClientDetailVO> detail(String id) {
        TClient tClient = clientMapper.selectByPrimaryKey(id);
        ResClientDetailVO resClientDetailVO = BeanHelper.copyProperties(tClient, ResClientDetailVO.class);
        return ResultDTO.okOf(resClientDetailVO);
    }

    @Override
    public ResultDTO<ResPageVO<ResClientDetailVO>> listOfPage(ReqPageVO vo) {
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        List<ResClientDetailVO> list =  clientMapper.list(vo.getSearchParem());
        PageInfo<ResClientDetailVO> pageInfo = new PageInfo<>(list);
        ResPageVO<ResClientDetailVO> resPageVO = new ResPageVO<>();
        resPageVO.setRows(pageInfo.getList());
        resPageVO.setCurrentPage(pageInfo.getPageNum());
        resPageVO.setTotal((int) pageInfo.getTotal());
        return ResultDTO.okOf(resPageVO);
    }

    @Override
    public ResultDTO<List<ResClientDetailVO>> list() {
        List<TClient> tClients = clientMapper.selectAll();
        if(CollectionUtils.isEmpty(tClients)){
            return ResultDTO.okOf(new ArrayList<>());
        }
        List<ResClientDetailVO> resClientDetailVOS = BeanHelper.copyWithCollection(tClients, ResClientDetailVO.class);
        return ResultDTO.okOf(resClientDetailVOS);
    }
}
